package Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import Dominion.*;

public class PublisherDAO extends CrudDAO<Publisher>{

    @Override
    public Publisher create(Publisher publi) {
        String sqlQuerry = "INSERT INTO Publisher (email, journal, web) VALUES ('"
        		+publi.getEmail()+"','"+publi.getJournal()+"','"+publi.getWeb()+"')";
        try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return null;
    }

    @Override
    public void delete(Publisher publi) {
    	 try {
 			AgenteBD.getDbCon().queryUpdate("DELETE FROM Publisher WHERE email='"+publi.getEmail()+"'");
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
    }

    @Override
    public Publisher read(Publisher publi) throws SQLException {
      ResultSet rs = AgenteBD.getDbCon().query(
                "SELECT * FROM Publisher WHERE email=" + publi.getEmail()+ "");
        if (rs != null) {
            try {
                while (rs.next()) {
                    publi.setJournal(rs.getString("journal"));
                    publi.setWeb(rs.getString("web"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        rs.close();
        return publi;
    }

    @Override
    public void update(Publisher publi) {
    	 String sqlQuerry="UPDATE Publisher SET journal='"+publi.getJournal()
		         +"', web='" + publi.getWeb()
		         + "' WHERE email='" + publi.getEmail()+ "'";
		try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}       
    }
}
